package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.rmi.dgc.Lease;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomAttrValueMapper roomAttrValueMapper;
    @Autowired
    private RoomFacilityMapper roomFacilityMapper;
    @Autowired
    private RoomLabelMapper roomLabelMapper;
    @Autowired
    private RoomPaymentTypeMapper roomPaymentTypeMapper;
    @Autowired
    private RoomLeaseTermMapper roomLeaseTermMapper;
    @Autowired
    private LeaseAgreementService leaseAgreementService;
    @Override
    public void customSaveOrUpdate(RoomSubmitVo roomSubmitVo) {
        //判断是更新还是保存
        boolean isUpdate = roomSubmitVo.getId() != null;
        //如果是更新就先删除在保存(房间图片,房间属性,房间配套,房间标签,房间支付方式,房间租期方式)
        //获取房间id
        Long roomId = roomSubmitVo.getId();
        //先操作主表
        saveOrUpdate(roomSubmitVo);
        if (isUpdate){
          //删除房间图片
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,roomId);
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);
            //删除房间属性
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,roomId);
            roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);
            //删除房间配套
            LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId,roomId);
            roomFacilityService.remove(roomFacilityLambdaQueryWrapper);
            //删除房间标签
            LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId,roomId);
            roomLabelService.remove(roomLabelLambdaQueryWrapper);
            //删除房间支付方式
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,roomId);
            roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);
            //删除房间租期方式
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,roomId);
            roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
        }
        //保存中间表数据
        //保存房间图片
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (CollectionUtils.isEmpty(graphVoList)){
            ArrayList<GraphInfo> graphInfos = new ArrayList<>(graphVoList.size());
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemId(roomId);
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setName(graphVo.getName());
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }
        //保存房间属性
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (CollectionUtils.isEmpty(attrValueIds)){
            ArrayList<RoomAttrValue> roomAttrValues = new ArrayList<>(attrValueIds.size());
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue build = RoomAttrValue
                        .builder()
                        .roomId(roomId)
                        .attrValueId(attrValueId)
                        .build();
                roomAttrValues.add(build);
            }
            roomAttrValueService.saveBatch(roomAttrValues);
        }
        //保存房间配套
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (CollectionUtils.isEmpty(facilityInfoIds)){
            ArrayList<RoomFacility> roomFacilities = new ArrayList<>(facilityInfoIds.size());
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility build = RoomFacility
                        .builder()
                        .roomId(roomId)
                        .facilityId(facilityInfoId)
                        .build();
                roomFacilities.add(build);
            }
            roomFacilityService.saveBatch(roomFacilities);
        }
        //保存房间标签
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (CollectionUtils.isEmpty(labelInfoIds)){
            ArrayList<RoomLabel> roomLabels = new ArrayList<>(labelInfoIds.size());
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel build = RoomLabel
                        .builder()
                        .roomId(roomId)
                        .labelId(labelInfoId)
                        .build();
                roomLabels.add(build);
            }
            roomLabelService.saveBatch(roomLabels);
        }
        //保存房间租期方式
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (CollectionUtils.isEmpty(leaseTermIds)){
            ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>(leaseTermIds.size());
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm build = RoomLeaseTerm
                        .builder()
                        .roomId(roomId)
                        .leaseTermId(leaseTermId)
                        .build();
                roomLeaseTerms.add(build);
            }
            roomLeaseTermService.saveBatch(roomLeaseTerms);
        }
        //保存房间支付方式
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (CollectionUtils.isEmpty(paymentTypeIds)){
            ArrayList<RoomPaymentType> roomPaymentTypes = new ArrayList<>(paymentTypeIds.size());
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType build = RoomPaymentType
                        .builder()
                        .roomId(roomId)
                        .paymentTypeId(paymentTypeId)
                        .build();
                roomPaymentTypes.add(build);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypes);
        }
    }

    @Override
    public void customPage(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        roomInfoMapper.customPage(page,queryVo);
    }

    @Override
    public RoomDetailVo customById(Long id) {
        //查询房间信息(房间图片,房间配套,房间标签,房间支付方式,房间租期方式,房间属性)
        //先查询房间主表信息
        RoomInfo roomInfo = getById(id);
        //查询进房间图片
          List<GraphVo> graphVoList = graphInfoMapper.customgetById(ItemType.ROOM,id);
          //查询房间配套
         List<FacilityInfo> facilityInfoList = roomFacilityMapper.customgetById(id);
         //查询房间标签
         List<LabelInfo> labelInfoList = roomLabelMapper.customgetById(id);
         //查询房间支付方式
         List<PaymentType>  paymentTypeList = roomPaymentTypeMapper.customgetById(id);
         //查询房间租期方式
         List<LeaseTerm> leaseTermList = roomLeaseTermMapper.customgetById(id);
         //查询房间属性
          List<AttrValueVo> attrValueVoList = roomAttrValueMapper.customgetById(id);
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        return roomDetailVo;
    }

    @Override
    public void customRemoveById(Long id) {
        //先判断房间有没有租约状态
        LambdaQueryWrapper<LeaseAgreement> leaseAgreementLambdaQueryWrapper = new LambdaQueryWrapper<>();
        leaseAgreementLambdaQueryWrapper.eq(LeaseAgreement::getRoomId,id);
        long count = leaseAgreementService.count(leaseAgreementLambdaQueryWrapper);
        if (count>0){
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        //先删除房间主表
        removeById(id);
        //删除房间图片
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.ROOM);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);
        //删除房间配套
        LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId,id);
        roomFacilityService.remove(roomFacilityLambdaQueryWrapper);
        //删除房间标签
        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId,id);
        roomLabelService.remove(roomLabelLambdaQueryWrapper);
        //删除房间支付防暑
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,id);
        roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);
        //删除房间租期方式
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,id);
        roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
        //删除房间属性
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,id);
        roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);
    }
}




































